/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadQuestaoPainel.java
 *
 * Created on 19/07/2011, 07:35:14
 */

package sad.visao;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import sad.modelo.Alternativa;
import sad.util.DAOEvent;

/**
 *
 * @author UAB-2010
 */
public class CadQuestaoDialog extends DialogAbstrato {

    private ArrayList listaAlternativa = new ArrayList();
/** Creates new form CadQuestaoPainel */
    public CadQuestaoDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        btAddAlternativa = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btSalvarQuestao = new javax.swing.JButton();
        txtEnunciado = new javax.swing.JTextField();
        txtJustificativa = new javax.swing.JTextField();
        txtFonte = new javax.swing.JTextField();
        listaSubTopico = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        alternativa = new javax.swing.JList();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jLabel3.setText("Sub-Tópico:");

        btAddAlternativa.setText("Add Alternativas");
        btAddAlternativa.setActionCommand("Nova Alternativa");

        jLabel1.setText("Fonte:");

        jLabel5.setText("Justificativa:");

        jLabel2.setText("Enunciado:");

        btSalvarQuestao.setText("Salvar Questão");

        jLabel4.setText("Alternativas:");

        jScrollPane3.setViewportView(alternativa);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                    .addComponent(txtJustificativa, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                    .addComponent(txtEnunciado, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtFonte, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btAddAlternativa)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                        .addComponent(btSalvarQuestao))
                    .addComponent(listaSubTopico, javax.swing.GroupLayout.Alignment.LEADING, 0, 538, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtFonte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(listaSubTopico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtEnunciado, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addGap(3, 3, 3)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btAddAlternativa)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtJustificativa, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btSalvarQuestao)
                    .addComponent(lblMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList alternativa;
    private javax.swing.JButton btAddAlternativa;
    private javax.swing.JButton btSalvarQuestao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JComboBox listaSubTopico;
    private javax.swing.JTextField txtEnunciado;
    private javax.swing.JTextField txtFonte;
    private javax.swing.JTextField txtJustificativa;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelDAOChange(DAOEvent evt) {
            switch(evt.getID()){
           case DAOEvent.DAO_CREATE:
                resetCadQuestao();
                lblMessage.setForeground(Color.GREEN);
                lblMessage.setText("Questão"
                    + " cadastrada com sucesso!!!");
                break;
               
           case DAOEvent.DAO_EXISTE:
               JOptionPane.showMessageDialog(this, "A Questão "
                    + evt.getSource().toString() + " já existe!!!", null, 
                                            JOptionPane.WARNING_MESSAGE);
                resetCadQuestao();
                break;
               
           case DAOEvent.DAO_ERROFIELD:
               JOptionPane.showMessageDialog(this, "O campo " + evt.getField()
                       + " não pode ficar em branco!!!", null,
                                    JOptionPane.WARNING_MESSAGE);
               focusField(evt.getField());
               break;
            }
    }
    
    @Override
    public void addOuvintes() {
        btAddAlternativa.addActionListener(getSuporteEventos());
        btSalvarQuestao.addActionListener(getSuporteEventos());
    }

    @Override
    public void setEditableDados(Object[] q) {
                if(q!=null){
            txtFonte.setText(q[0].toString());
            listaSubTopico.setSelectedItem(q[1].toString());
            txtEnunciado.setEnabled(false);
            btAddAlternativa.setActionCommand("Alterar");
            btSalvarQuestao.setBorder(
                    javax.swing.BorderFactory.createTitledBorder("Alterar Questão"));
        }

    }
    
    private void resetCadQuestao() {
        txtEnunciado.setText(null);
        txtFonte.setText(null);
        txtJustificativa.setText(null);
        lblMessage.setText(null);
        txtFonte.requestFocusInWindow();
    }
    
    @Override
    protected void focusField(String field) {
        if(field.equals("Fonte")){
           txtFonte.requestFocusInWindow();
        }
        if(field.equals("Enunciado")){
           txtEnunciado.requestFocusInWindow();
        }
        if(field.equals("Sub Topico")){
           listaSubTopico.requestFocusInWindow();
        }
        if(field.equals("Justificativa")){
           txtJustificativa.requestFocusInWindow();
        }
    }
    
    public String getFonte() {
        return this.txtFonte.getText();
    }
    
    public String getEnuciado() {
        return this.txtEnunciado.getText();
    }

    public String getJustificativa() {
        return this.txtJustificativa.getText();
    }
    
    public String getSubTopico(){
        return (String) listaSubTopico.getSelectedItem();
    }
    public ArrayList getListaAlternativa(){  
        return listaAlternativa;
    }
    public void setAlternativa(Alternativa alternativa){  
        listaAlternativa.add(WIDTH, alternativa);
    }

    @Override
    public void close() {
        this.resetCadQuestao();
        this.dispose();
    }
    
    public void reload(Vector<String> s){
        listaSubTopico.setModel(new DefaultComboBoxModel(s));
    }
}
